﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMITDomain.Abstract;
using MMITDomain.Entities;

namespace MMITDomain.Concrete
{
    public class EFCountryRepository : ICountryRepository
    {
        EFDbContext dbContext = new EFDbContext();

        #region ICountryRepository Members

        public virtual IQueryable<Entities.Country> Countries
        {
            get
            {
                return dbContext.Countries;
            }
        }

        public virtual void SaveCountry(Country objCountry)
        {
            if (objCountry.Id == 0)
            {
                dbContext.Countries.Add(objCountry);
            }

            dbContext.SaveChanges();
        }

        public void DeleteCountry(Country objCountry)
        {
            dbContext.Countries.Remove(objCountry);

            dbContext.SaveChanges();
        }

        public virtual Country GetCountryById(int iCountryId)
        {
            return dbContext.Countries.Find(iCountryId);
        }

        #endregion

        public IEnumerable<Currency> GetCurrencies(Country objCountry)
        {
            return objCountry.Currencies;
        }

        public Currency GetDefaultCurrency(Country objCountry)
        {
            var p = (from t in dbContext.CountryCurrencies
                    where t.Country.Id == objCountry.Id
                    && t.Default == true
                    select t).FirstOrDefault();

            return new EFCurrencyRepository().GetCurrencyById(p.CurrencyId);
        }
    }
}
